package dao

import (
	__ "base_srv/basic/baseproto"
	"base_srv/handler/models"
	"errors"
	"time"
)

// 组织添加
func OrganizationAdd(in *__.OrganizationAddReq) (uint32, error) {
	registerTime, err := time.Parse("2006-01-02 15:04:05", in.RegisterTime)
	if err != nil {
		return 0, errors.New("注册时间格式错误")
	}
	o := models.OrganizationInfo{
		OrganizationName:   in.OrganizationName,
		OrganizationCode:   in.OrganizationCode,
		AdministrativeCode: in.AdministrativeCode,
		ServiceAddress:     in.ServiceAddress,
		HeaderName:         in.HeaderName,
		HeaderMobile:       in.HeaderMobile,
		ManagerName:        in.ManagerName,
		ManagerMobile:      in.ManagerMobile,
		OrganizationMobile: in.OrganizationMobile,
		MailingAddress:     in.MailingAddress,
		RegisterTime:       registerTime.Add(-8 * time.Hour),
		Status:             int8(in.Status),
	}
	if err = o.Create(); err != nil {
		return 0, errors.New("添加组织失败")
	}
	return o.Id, nil
}

// 组织更新
func OrganizationUpdate(in *__.OrganizationUpdateReq) error {
	registerTime, err := time.Parse("2006-01-02 15:04:05", in.RegisterTime)
	if err != nil {
		return errors.New("注册时间格式错误")
	}
	o := models.OrganizationInfo{
		OrganizationName:   in.OrganizationName,
		OrganizationCode:   in.OrganizationCode,
		AdministrativeCode: in.AdministrativeCode,
		ServiceAddress:     in.ServiceAddress,
		HeaderName:         in.HeaderName,
		HeaderMobile:       in.HeaderMobile,
		ManagerName:        in.ManagerName,
		ManagerMobile:      in.ManagerMobile,
		OrganizationMobile: in.OrganizationMobile,
		MailingAddress:     in.MailingAddress,
		RegisterTime:       registerTime.Add(-8 * time.Hour),
		Status:             int8(in.Status),
		CreateTime:         time.Time{},
		UpdateTime:         time.Time{},
		DeleteTime:         time.Time{},
	}
	if err = o.Updates(); err != nil {
		return errors.New("更新组织失败")
	}
	return nil
}

// 经营许可证信息添加
func BusinessLicenseInformationAdd(in *__.BusinessLicenseInformationAddReq) (uint32, error) {
	licenseStartDate, err := time.Parse("2006-01-02", in.LicenseStartDate)
	if err != nil {
		return 0, errors.New("经营许可证开始时间格式错误")
	}
	licenseEndDate, err := time.Parse("2006-01-02", in.LicenseEndDate)
	if err != nil {
		return 0, errors.New("经营许可证结束时间格式错误")
	}
	issuanceDate, err := time.Parse("2006-01-02", in.IssuanceDate)
	if err != nil {
		return 0, errors.New("经营许可证发证时间格式错误")
	}
	o := models.BusinessLicenseInformation{
		OrganizationName:     in.SendOrganizationName,
		AdministrativeCode:   in.AdministrativeCode,
		PermitCode:           in.PermitCode,
		BusinessScope:        in.BusinessScope,
		SendOrganizationName: in.SendOrganizationName,
		LicenseStartDate:     licenseStartDate.Add(-8 * time.Hour),
		LicenseEndDate:       licenseEndDate.Add(-8 * time.Hour),
		IssuanceDate:         issuanceDate.Add(-8 * time.Hour),
		Status:               int8(in.Status),
	}
	if err = o.Create(); err != nil {
		return 0, errors.New("添加经营许可证信息失败")
	}
	return o.Id, nil
}

// 经营许可证信息更新
func BusinessLicenseInformationUpdate(in *__.BusinessLicenseInformationUpdateReq) error {
	o := models.BusinessLicenseInformation{
		Id:                   in.Id,
		OrganizationName:     in.SendOrganizationName,
		AdministrativeCode:   in.AdministrativeCode,
		PermitCode:           in.PermitCode,
		BusinessScope:        in.BusinessScope,
		SendOrganizationName: in.SendOrganizationName,
		Status:               int8(in.Status),
	}
	if in.LicenseStartDate != "" {
		licenseStartDate, err := time.Parse("2006-01-02", in.LicenseStartDate)
		if err != nil {
			return errors.New("经营许可证开始时间格式错误")
		}
		o.LicenseStartDate = licenseStartDate.Add(-8 * time.Hour)
	}
	if in.LicenseEndDate == "" {
		licenseEndDate, err := time.Parse("2006-01-02", in.LicenseEndDate)
		if err != nil {
			return errors.New("经营许可证结束时间格式错误")
		}
		o.LicenseEndDate = licenseEndDate.Add(-8 * time.Hour)
	}
	if in.IssuanceDate != "" {
		issuanceDate, err := time.Parse("2006-01-02", in.IssuanceDate)
		if err != nil {
			return errors.New("经营许可证发证时间格式错误")
		}
		o.IssuanceDate = issuanceDate.Add(-8 * time.Hour)
	}
	if err := o.Updates(); err != nil {
		return errors.New("更新经营许可证信息失败")
	}
	return nil
}
